package com.netintech.project.system.controller.fastDfs;

import com.netintech.common.utils.FileDfsUtil;
import com.netintech.core.web.domain.RequestResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
@Api(tags = "FastDFS附件操作接口", description = "FastDFS附件操作接口")
@RestController
public class FileController {
    @Resource
    private FileDfsUtil fileDfsUtil ;

    @ApiOperation(value="上传文件接口", notes="FastDFS文件上传")
    @RequestMapping(value = "/uploadFile",headers="content-type=multipart/form-data", method = RequestMethod.POST)
    public RequestResult uploadFile (@RequestParam("file") MultipartFile file){
        String result ;
        try{
            String path = fileDfsUtil.upload(file) ;
            if (!StringUtils.isEmpty(path)){
                result = path ;
            } else {
                result = "上传失败" ;
            }
        } catch (Exception e){
            e.printStackTrace() ;
            result = "服务异常" ;
        }
        return RequestResult.success(result);
    }
    /**
     * 文件删除
     */
    @ApiOperation(value="删除fastDFS中附件", notes="删除fastDFS中附件")
    @ApiImplicitParams({
            @ApiImplicitParam(name="filePathName",value="附件路径",required=true,paramType="query",dataType="String")
    })
    @RequestMapping(value = "/deleteByPath", method = RequestMethod.GET)
    public RequestResult deleteByPath (String filePathName){
        String result = fileDfsUtil.deleteFile(filePathName);
        if("OK".equals(result)){
            return RequestResult.success("删除成功");
        }else{
            return  RequestResult.error(result);
        }
    }




}
